#delimit;
clear;
set more off;
capture log close;
set more off;

/****************************
This program creates 2 datasets:
One with the variables that calculate the federal loans disbursed by age
One that creates degrees and majors obtained by age
*****************************/
/*****
CHANGE PATH
*****/;
local out "/data";
local pathtab2 "/data";


/*Create degrees by age*/
use "`out'/NSC_merged.dta", clear;

sort pid Graduation_Date;
keep if Graduated=="Y";
keep pid DEG_GRP major_group Graduation_Date;

rename DEG_GRP degree;
rename Graduation_Date grad_date;

merge m:1 pid using "`pathtab2'/age_tomerge.dta";
drop _m;

gen degree_type=.;
replace degree_type = 4 if degree=="BACHELOR";
replace degree_type = 6 if (degree=="DOCTOR" | degree =="LAW" | degree=="MASTER" | degree=="MBA");
replace degree_type = 2 if (degree=="ASSOCIATE" | degree=="CERTIFICATE");
replace degree_type = 1 if (degree=="OTHER" | degree=="HIGH SCHOOL");
replace degree_type = 0 if degree_type==.;

sort pid grad_date degree_type;
replace degree_type=3 if degree_type==1 & degree_type[_n-1]==2 & pid[_n-1]==pid[_n]; //At least AS
replace degree_type=5 if degree_type==1 & degree_type[_n-1]==4 & pid[_n-1]==pid[_n]; //At least BA

label define degree 0 "0=No degree" 1 "1=Degree Unknown" 2 "2=Associate/Certificate" 3 "3=At least Associate/Certificate" 4 "4=Bachelor" 5 "5=At least BA" 6 "6=More than BA";

gen count=_n;

/*I will use information from NSLDS to impute degrees*/
merge m:m pid using "`pathtab2'/nslds_tomerge_degree.dta";
drop _m;

sort count;
gen degree_type_alt_t=4 if grad_date~=. & (degree_type==1 | degree_type==3) & acad_lvl_num==4 & per_beg_dt<grad_date;
replace degree_type_alt_t=2 if grad_date~=. & degree_type==1 & acad_lvl_num==2 & per_beg_dt<grad_date;
replace degree_type_alt_t=5 if grad_date~=. & (degree_type==1 | degree_type==3) & acad_lvl_num>=6  & acad_lvl_num<=10 & per_beg_dt<grad_date;

sort count;
by count: egen degree_type_alt=max(degree_type_alt_t);

drop degree_type_alt_t;

by count: keep if _n==1;
replace degree_type=degree_type_alt if degree_type_alt~=.;

sort pid grad_date;
forvalue yo=22(1)41 {;
gen degree_t_`yo'=0;
replace degree_t_`yo'=degree_type if (grad_date-dob)/365<`yo';
by pid: egen degree_`yo'=max(degree_t_`yo');
local yo_1=`yo'-1;
rename degree_`yo' degree_`yo_1';
drop degree_t_`yo';
label values degree_`yo_1' degree;
};

drop acad_lvl_num per_beg_dt per_end_dt count degree_type_alt;
/*******************************/
/*Now I will define majors*/
/*First, I'm going to replace major_group=40 ad major_group=32. After all, both are
going to be treated as not-known*/
/*******************************/
replace major_group=32 if major_group==40;
replace major_group=2 if major_group==9;
replace major_group=15 if major_group==30 | major_group==25;
replace major_group=4 if major_group==22 | major_group==20 | major_group==1;
replace major_group=6 if major_group==7;
replace major_group=11 if major_group==17 | major_group==27 | major_group==3 | major_group==23 | major_group==16;
replace major_group=13 if major_group==14 | major_group==21 | major_group==26;

/*Now I will drop observations that have the same major for the same day*/
egen major_dup=group(pid grad_date major_group);
bys major_dup: drop if _n~=1 & major_dup~=.;
drop major_dup;

gen major_type=.;
replace major_type=major_group;

egen degree_date=group(pid grad_date);
drop if ((degree_date==degree_date[_n-1]) | (degree_date==degree_date[_n+1])) & degree_date~=. & major_group==32;

sort pid grad_date;
gen person=1;
by pid: egen sum_person=sum(person);

forvalue yo=22(1)41 {;
sort pid grad_date degree_type;
gen major_t_`yo'=0;
replace major_t_`yo'=major_type if (grad_date-dob)/365<`yo';

gen major_`yo'=major_t_`yo' if sum_person==1;

gen count=1 if major_t_`yo'~=0;
by pid: egen sum_person_`yo'=sum(count);
replace major_`yo'=0 if sum_person_`yo'==0;
replace sum_person_`yo'=. if major_t_`yo'==0;
drop count;

gen sum_degree_`yo'_t=1 if ((degree_date==degree_date[_n-1]) | (degree_date==degree_date[_n+1])) & (grad_date-dob)/365>=`yo'-1 & (grad_date-dob)/365<`yo';
bys degree_date: egen sum_degree_`yo'=sum(sum_degree_`yo'_t); 
drop sum_degree_`yo'_t;

/*First, I will assign the major to those that only have one major until that point*/
replace major_`yo'=major_t_`yo' if sum_person_`yo'==1;

/*Now I will assign major as the latest the person got*/
gsort pid sum_person sum_person_`yo' -grad_date;
gen major_temp_`yo'=major_`yo';
drop major_`yo';
replace major_temp_`yo'=major_t_`yo' if major_temp_`yo'==. & sum_person_`yo'>=2 & sum_person_`yo'<. & grad_date>grad_date[_n+1] & pid==pid[_n+1] & pid~=pid[_n-1];
by pid: egen major_`yo'=max(major_temp_`yo');
drop major_temp_`yo';

gen major_temp_`yo'=major_`yo';
drop major_`yo';

/*I will hardcode the cases which I couldn't correct yet*/
/*Age 22*/
replace major_temp_`yo'=5 if pid=="9EA00800004152" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370100001574" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370100002639" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ370100002657" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370100005654" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370100009045" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370200000342" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200004521" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370200005414" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370300000646" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370300006070" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370300007537" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370300008201" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ370400002274" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370400005049" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370400005396" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370500004817" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370500005871" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370500009732" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ370600001353" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=3 if pid=="CQ370600001504" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370600010357" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370700000994" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370700001306" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370700004720" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370800005261" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371200003745" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ371200004581" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=22 if pid=="CQ371300001453" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ371400005956" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371400006694" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371500007956" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371600001433" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ371600001739" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371700005234" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371800004435" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371800006500" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=14 if pid=="CQ371800007264" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ371900006544" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372100009702" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372200000167" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372200002526" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372200005082" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372200009375" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372300008349" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=9 if pid=="CQ372300009191" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372300009248" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372400005448" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400005527" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372400007295" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372500003994" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372800003688" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373100004725" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373100006971" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373200001549" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373400000814" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ373400004939" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ373400005245" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ373500004594" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=30 if pid=="CQ373500008178" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373600002023" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373700000310" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373700000457" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373700000523" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373700006823" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373800002172" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373800004231" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=25 if pid=="CQ373800004676" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373900003228" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373900004568" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370900003501" & major_t_`yo'~=0 & major_temp_`yo'==.;


/*Age 23*/
replace major_temp_`yo'=17 if pid=="CQ370100001694" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370100001715" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370100003432" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370100004139" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370100004175" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370100005581" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370100009083" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=16 if pid=="CQ370200001543" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200001928" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200003273" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200003285" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200004675" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370200006112" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200006319" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370200006446" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370300002110" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370300006665" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370300007762" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370300008381" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370400005417" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370400007001" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370400007820" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370500000674" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370500002753" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370500005083" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370500006126" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370600004808" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370600005796" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=30 if pid=="CQ370600007426" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370600009536" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370700001144" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370700002571" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370700004136" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370700004270" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370700004359" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370700005424" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370700005493" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ370700005751" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370700006979" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370700007548" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370700008442" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370800001137" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370800002558" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370900007119" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ370900007324" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ371100005745" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ371200000297" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371200007901" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371200008459" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371300002689" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ371300003973" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371300007937" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ371400003047" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371500004509" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371600001941" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371700002455" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371700002549" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ371700005730" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371700006691" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ371700007862" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ371800000478" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371800002187" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371800005536" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371800005821" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371800008441" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ371800008459" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ371900002165" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ371900004486" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371900005157" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=14 if pid=="CQ371900007134" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ371900007934" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=14 if pid=="CQ372100001229" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372100002049" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372100004533" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372100005564" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ372100009077" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372200000132" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372200005160" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372200007272" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372200007839" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372200008318" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372200009566" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372300001171" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372300001344" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372300002648" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372300004333" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=20 if pid=="CQ372300004416" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372300006370" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372300007598" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372300007772" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372400002640" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400002723" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400002852" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372400004694" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372500001606" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ372500002538" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372500002911" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372500006671" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372500008931" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372600001675" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372600005378" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372700006978" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372700007300" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372700007992" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372700008690" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372800004400" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372800006691" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372800007197" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372900001365" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ373100001404" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373100006084" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=25 if pid=="CQ373100006587" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373200002143" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373200006961" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ373200006982" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373200007476" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373200008137" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373300002830" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=20 if pid=="CQ373400001086" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=16 if pid=="CQ373600000051" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373600001601" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373600003371" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373600008342" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373600008868" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373600008926" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373700001077" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=25 if pid=="CQ373700001284" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373700001351" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373700001356" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ373700001560" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373700003186" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373700003905" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373700004967" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373800005198" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373900002504" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373900002705" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373900005263" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373700005108" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 24*/
replace major_temp_`yo'=15 if pid=="CQ370100000820" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370100003674" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370100004786" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370200002274" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370200007598" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370200008762" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370300000816" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370300004009" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370300005686" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ370400005195" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ370400005472" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370600002907" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370600004791" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ370600009805" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370700003218" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370700008320" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370700008642" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ370800000737" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ370800001101" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ370800005839" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ370900007082" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=30 if pid=="CQ371100000592" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371100003741" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371200003964" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371300005293" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371400001905" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ371400006615" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371600001942" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ371700001183" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ371700005171" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371700008018" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371900001083" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ371900005263" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ371900009325" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372100008567" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372200000886" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ372200000964" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ372200002115" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372200003201" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372200007277" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372200009359" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ372300002190" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372300004569" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372300004800" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372300005164" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372300008263" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372500006684" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372500008176" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372500009165" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372600001805" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372700007612" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ372900001091" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ372900006281" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373100004619" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373100004838" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373100006779" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373100006840" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373200000852" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373200007290" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373200007701" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373500001785" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373500002197" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373500006811" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=30 if pid=="CQ373600003152" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373600003826" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373600007187" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=25 if pid=="CQ373700001374" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373800001310" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373800008755" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373800008839" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373900006536" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=14 if pid=="CQ373900008696" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373900008860" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ372500002863" & major_t_`yo'~=. & major_temp_`yo'==.;

/*Age 25*/
replace major_temp_`yo'=12 if pid=="CQ370300002654" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ370400002339" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370500008701" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=20 if pid=="CQ370600009359" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ370700003131" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ370900007276" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ371100006241" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371800002964" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372100009152" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372200000913" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ372200008874" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ372800002999" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=25 if pid=="CQ373100005526" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373200000685" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=3 if pid=="CQ373200005344" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373300001448" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373600003617" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373600007593" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373700002995" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373700007841" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373800000834" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373900003217" & major_t_`yo'~=. & major_temp_`yo'==.;

/*Age 26*/
replace major_temp_`yo'=12 if pid=="CQ370100000643" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370300005496" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370400008582" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ370500001261" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ370500005192" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370500007899" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=19 if pid=="CQ371500006594" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371700008983" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371800008958" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ372300003204" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372300004577" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ372400005552" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ372500001266" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ373300001165" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=17 if pid=="CQ373700004015" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=27 if pid=="CQ373800007026" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373900001793" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373900001830" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=14 if pid=="CQ373900003804" & major_t_`yo'~=. & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373900004260" & major_t_`yo'~=. & major_temp_`yo'==.;

/*Age 27*/
replace major_temp_`yo'=11 if pid=="CQ370200007683" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370800005660" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ371100005071" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371600008954" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371700003164" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371900001231" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372200001537" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ372200007250" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373100005588" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ373100007338" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373800003320" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 28*/
replace major_temp_`yo'=15 if pid=="CQ370100005439" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371700006914" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371700008716" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400002587" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373100002255" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373100008344" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373200005278" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373500002883" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373600000261" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373600009092" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373900002726" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 29*/
replace major_temp_`yo'=12 if pid=="CQ370200004425" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ370200004633" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ370600003693" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371200000045" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371200002521" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371300005449" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371900008591" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ372100003085" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ372500000303" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372800006126" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373300007311" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373900000752" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373900002749" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 30*/
replace major_temp_`yo'=19 if pid=="9EA00700008311" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370300002644" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370500009951" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370800005133" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371300005636" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371500005289" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372300006937" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373100008167" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ373300003311" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ373600008664" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373700003916" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373700006781" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 31*/
replace major_temp_`yo'=5 if pid=="CQ370100003701" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ371400001020" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371400006832" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400007311" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ372500006008" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373400000619" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373500004699" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373600005857" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373800005722" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373900005317" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 32*/
replace major_temp_`yo'=15 if pid=="CQ370200004432" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ370800007475" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371500003541" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=15 if pid=="CQ371800007275" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ373200006752" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 33*/
replace major_temp_`yo'=13 if pid=="9EA00600009090" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ370100006723" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370400007561" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ370700008947" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370900000296" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371200007151" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371400007449" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371600000780" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371800008487" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=28 if pid=="CQ371900001254" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371900007881" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ373400008182" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373600003057" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373700006396" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373800002066" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 34*/
replace major_temp_`yo'=18 if pid=="CQ370300004259" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371100006788" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ372100002650" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ373200002679" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=6 if pid=="CQ373200005976" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ373600008417" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 35*/
replace major_temp_`yo'=15 if pid=="CQ370300001720" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ370800004060" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371600001954" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371800006474" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ371900004840" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400000115" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=18 if pid=="CQ372500000885" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373300005659" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ373600009018" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 36*/
replace major_temp_`yo'=11 if pid=="CQ370100000503" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ371100007580" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372200001549" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ372400009190" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 37*/
replace major_temp_`yo'=4 if pid=="CQ370600005553" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=5 if pid=="CQ371200005147" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=31 if pid=="CQ373500005902" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 38*/
replace major_temp_`yo'=13 if pid=="CQ370800002956" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371100005097" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ371200006707" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=11 if pid=="CQ371500006246" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ372300002544" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=10 if pid=="CQ373200008474" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ373700007913" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=8 if pid=="CQ373800006254" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 39*/
replace major_temp_`yo'=12 if pid=="CQ370800005005" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=13 if pid=="CQ371200007865" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ371900000067" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 40*/
replace major_temp_`yo'=12 if pid=="CQ370800005433" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=4 if pid=="CQ371900004244" & major_t_`yo'~=0 & major_temp_`yo'==.;
replace major_temp_`yo'=12 if pid=="CQ372900004766" & major_t_`yo'~=0 & major_temp_`yo'==.;

/*Age 41*/
replace major_temp_`yo'=13 if pid=="CQ371700004518" & major_t_`yo'~=0 & major_temp_`yo'==.;
by pid: egen major_`yo'=max(major_temp_`yo');
drop major_temp_`yo';

gen major_agg_`yo'=0 if major_`yo'==0;
replace major_agg_`yo'=1 if major_`yo'==2 | major_`yo'==9;
replace major_agg_`yo'=2 if major_`yo'==15 | major_`yo'==30 | major_`yo'==25;
replace major_agg_`yo'=3 if major_`yo'==4 | major_`yo'==22 | major_`yo'==20 | major_`yo'==1;
replace major_agg_`yo'=4 if major_`yo'==6 | major_`yo'==7;
replace major_agg_`yo'=5 if major_`yo'==8;
replace major_agg_`yo'=6 if major_`yo'==10;
replace major_agg_`yo'=7 if major_`yo'==11 | major_`yo'==17 | major_`yo'==27 | major_`yo'==3 | major_`yo'==23 | major_`yo'==16;
replace major_agg_`yo'=8 if major_`yo'==12;
replace major_agg_`yo'=9 if major_`yo'==13 | major_`yo'==14 | major_`yo'==21 | major_`yo'==26;
replace major_agg_`yo'=10 if major_`yo'==5;
replace major_agg_`yo'=11 if major_`yo'==18;
replace major_agg_`yo'=12 if major_`yo'==19;
replace major_agg_`yo'=13 if major_`yo'==28;
replace major_agg_`yo'=14 if major_`yo'==31;
replace major_agg_`yo'=15 if major_`yo'==32 | major_`yo'==24 | major_`yo'==29 | major_`yo'==40;

local yo_1=`yo'-1;
rename major_`yo' major_`yo_1';
rename major_agg_`yo' major_agg_`yo_1';
};

keep pid degree_2* degree_3* degree_4* major_2* major_3* major_4* major_agg_2* major_agg_3* major_agg_4*;
bys pid: keep if _n==1;

save "`pathtab2'/degree_major_tomerge.dta", replace;
